import language from '../language'
import resource from "../language/ExpenseReimburse_I18n_hu";

let isNull = function (obj) {
  if (obj === null || typeof obj === 'undefined' || obj === '') {
    return true
  }
  return false
}
let mixins = {
  data: function () {
    return {}
  },
  methods: {
    alert (msg) {
      let inf = this.getResource('alert.header.inf', this)
      return this.$alert(msg, inf, {showClose: false})
    },
    tips (msg) {
      this.$notify({
        message: msg,
        duration: 5000 // ms
      })
    },
    getResource (key, me) {
      if (typeof me === 'undefined') {
        me = dora
      }
      if (key.indexOf('alert.') !== -1 ||
        key.indexOf('title.') !== -1 ||
        key.indexOf('label.') !== -1) {
        if (me.$store.state.lang === '') {
          me.$store.state.lang = 'zh'
        }
        if (typeof language[me.$store.state.lang][key] !== 'undefined') {
          return language[me.$store.state.lang][key]
        } else {
          return key
        }
      } else {
        return key
      }
    },
    isNull: isNull,
    toLocaleDate (srcDate) {
      if (isNull(srcDate)) {
        return srcDate
      }
      var currDate = new Date()
      var timezoneOffSet = currDate.getTimezoneOffset() // 东八区-480

      srcDate = srcDate.replace(/-/g, '/')
      var d = new Date(srcDate)
      d.setMinutes(d.getMinutes() - timezoneOffSet)

      var year = d.getFullYear()
      var month = d.getMonth() + 1
      var day = d.getDate()
      var hour = d.getHours()
      var min = d.getMinutes()
      var sec = d.getSeconds()

      var dStr = year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec

      return dStr
    },
    getUuid () {
      var s = []
      var hexDigits = '0123456789abcdef'
      for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
      }
      s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
      s[8] = s[13] = s[18] = s[23] = '-'

      var uuid = s.join('')
      return uuid
    },
    countDate (after, before) {
      if (isNull(after) || isNull(before)) {
        return ''
      }
      after = after.replace(/-/g, '/')
      before = before.replace(/-/g, '/')

      var afterDate = new Date(after)
      var beforeDate = new Date(before)

      var count = afterDate - beforeDate
      var cutDay = Math.floor(count / 1000 / 60 / 60 / 24)
      return cutDay
    },
    getLanguage (key) {
      let lang = this.$store.state.lang
      if (key.indexOf('alert.') !== -1 ||
        key.indexOf('title.') !== -1 ||
        key.indexOf('label.') !== -1) {
        let lang2 = '' + lang
        if (!lang2 || lang2 === '') {
          lang2 = 'zh'
        }
        // console.log('lang2', lang2)
        if (typeof language[lang2][key] !== 'undefined') {
          return language[lang2][key]
        } else {
          return key
        }
      }
    },
    datetimeToString (date, format) {
      if (!date) {
        return ''
      }
      let aDate = new Date(date)
      var o = {
        'M+': aDate.getMonth() + 1, // month
        'd+': aDate.getDate(), // day
        'h+': aDate.getHours(), // hour
        'm+': aDate.getMinutes(), // minute
        's+': aDate.getSeconds(), // second
        S: aDate.getMilliseconds() // millisecond
      }
      if (/(y+)/.test(format)) {
        format = format.replace(
          RegExp.$1,
          (aDate.getFullYear() + '').substr(4 - RegExp.$1.length)
        )
      }
      for (var k in o) {
        if (new RegExp('(' + k + ')').test(format)) {
          format = format.replace(
            RegExp.$1,
            RegExp.$1.length === 1
              ? o[k]
              : ('00' + o[k]).substr(('' + o[k]).length)
          )
        }
      }
      // console.log(format);
      return format
    },
    splitTranslate: function (lang, realLanguage) {
      let array = realLanguage.split('@!')
      if (lang === 'zh') {
        return array[0]
      }
      return array[array.length - 1]
    }
    // ,
    // validInputLength (textInfo, inputLength, tips) {
    //   var infoLength = $(textInfo).val().length
    //   var remnant = inputLength - infoLength
    //   var text = $(textInfo).val()
    //   if (remnant < 0) {
    //     this.alert(bpmcoreI18n.msgInputvalueTooLong)
    //     // $(textInfo).val(($(textInfo).val()).substring(0, inputLength))
    //     // $('#' + tips).text('(0/' + inputLength + ')')
    //   } else {
    //     // $('#' + tips).text('(' + remnant + '/' + inputLength + ')')
    //   }
    // }

    // 'toast': function (msg) {
    //   modal.toast({
    //     message: msg,
    //     duration: 1
    //   })
    // },
    // 'alert': function (msg, callback, option) {
    //   let okTitle = '确定'
    //   if (option) {
    //     if (option.okTitle) { okTitle = option.okTitle }
    //   }
    //   modal.alert({
    //     message: msg,
    //     duration: 0.4,
    //     okTitle: okTitle
    //   }, function (value) {
    //     callback && callback(value)
    //   })
    // },
    // 'confirm': function (msg, callback, option) {
    //   let okTitle = '确定', cancelTitle = '取消'
    //   if (option) {
    //     if (option.okTitle) { okTitle = option.okTitle }
    //     if (option.cancelTitle) { cancelTitle = option.cancelTitle }
    //   }
    //   modal.confirm({
    //     message: msg,
    //     duration: 0.4,
    //     okTitle: okTitle,
    //     cancelTitle: cancelTitle
    //   }, function (value) {
    //     callback && callback(value)
    //   })
    // },
    // 'showAnimation': function (params, callback) {
    //   let el = params.id
    //   if (!el) {
    //     return
    //   }
    //   let duration = params.duration
    //   let transform = params.transform || 'translate(0, 0)'
    //   let transformOrigin = params.transformOrigin || 'center center'
    //   let timingFunction = params.timingFunction || 'ease'
    //
    //   animation.transition(el, {
    //     styles: {
    //       opacity: '1',
    //       transform: transform,
    //       transformOrigin: transformOrigin
    //     },
    //     duration: duration || 0,
    //     timingFunction: timingFunction,
    //     delay: 0
    //   }, function () {
    //     callback && callback()
    //   })
    // },
    // 'hide': function (params, callback) {
    //   let el = params.id
    //   if (!el) {
    //     return
    //   }
    //   let duration = params.duration
    //   let transform = params.transform || 'translate(0, 0)'
    //   let transformOrigin = params.transformOrigin || 'center center'
    //   let timingFunction = params.timingFunction || 'ease'
    //
    //   animation.transition(el, {
    //     styles: {
    //       opacity: '0',
    //       transform: transform,
    //       transformOrigin: transformOrigin
    //     },
    //     duration: duration || 0,
    //     timingFunction: timingFunction,
    //     delay: 0
    //   }, function () {
    //     callback && callback()
    //   })
    // },
    // 'getContextPath': function () {
    //   let url
    //   let bundleUrl = weex.config.bundleUrl
    //   url = bundleUrl.split('/').slice(0, -1).join('/')
    //   return url
    // },
    // 'getPageParams': function () {
    //   let params = {}
    //   let url = weex.config.bundleUrl
    //   let index = url.indexOf('?')
    //   if (index > 0) {
    //     let query = url.substring(index + 1)
    //     let temp = query.split('&')
    //     let key, value
    //     for (let p in temp) {
    //       if (temp[p]) {
    //         key = temp[p].split('=')[0]
    //         value = temp[p].split('=')[1]
    //         params[key] = decodeURIComponent(value)
    //       }
    //     }
    //   }
    //   return params
    // }
  }
}

export default mixins
